extends Node
class_name PickupManager

# 经验宝石场景
@export var exp_small_scene: PackedScene    # 小经验宝石
@export var exp_medium_scene: PackedScene   # 中经验宝石
@export var exp_large_scene: PackedScene    # 大经验宝石
@export var exp_xlarge_scene: PackedScene   # 超大经验宝石
@export var exp_xxlarge_scene: PackedScene  # 超超大经验宝石

# 其他可拾取物品
@export var coin_scene: PackedScene
@export var health_potion_scene: PackedScene

# 经验宝石生成分布
@export var small_exp_chance: float = 0.60   # 60%几率小经验
@export var medium_exp_chance: float = 0.25  # 25%几率中经验
@export var large_exp_chance: float = 0.10   # 10%几率大经验
@export var xlarge_exp_chance: float = 0.04  # 4%几率超大经验
@export var xxlarge_exp_chance: float = 0.01 # 1%几率超超大经验

# 经验值配置
@export var small_exp_value: int = 1
@export var medium_exp_value: int = 5
@export var large_exp_value: int = 20
@export var xlarge_exp_value: int = 50
@export var xxlarge_exp_value: int = 100

# 游戏管理器引用
var game_manager: Node

func _ready():
	# 尝试获取GameManager引用
	game_manager = get_node_or_null("/root/Main/GameManager")

# 在指定位置生成经验宝石
func spawn_experience(position: Vector2, base_value: int = 1):
	# 根据基础值和随机因素确定经验宝石类型
	var exp_type = determine_experience_type(base_value)
	var exp_value = get_experience_value(exp_type)
	
	# 根据类型选择正确的场景
	var exp_scene = get_experience_scene(exp_type)
	if exp_scene == null:
		push_error("Experience scene not set for type " + str(exp_type))
		return null
	
	# 创建经验宝石实例
	var exp_instance = exp_scene.instantiate()
	add_child(exp_instance)
	
	# 设置宝石属性
	exp_instance.global_position = position
	exp_instance.set_experience_value(exp_value)
	
	# 连接信号
	exp_instance.connect("collected", _on_experience_collected)
	
	return exp_instance

# 根据经验类型获取对应的场景
func get_experience_scene(exp_type: int) -> PackedScene:
	match exp_type:
		1: return exp_small_scene
		2: return exp_medium_scene
		3: return exp_large_scene
		4: return exp_xlarge_scene
		5: return exp_xxlarge_scene
		_: return exp_small_scene

# 根据基础值和随机性确定经验宝石类型（1=小，2=中，3=大，4=超大，5=超超大）
func determine_experience_type(base_value: int) -> int:
	# 基础值影响概率分布
	var value_factor = clamp(base_value / 10.0, 1.0, 3.0)
	
	# 对于强大的敌人，增加获得更好经验的几率
	var xxlarge_chance = xxlarge_exp_chance * value_factor
	var xlarge_chance = xlarge_exp_chance * value_factor
	var large_chance = large_exp_chance * value_factor
	var medium_chance = medium_exp_chance * value_factor
	
	# 随机确定类型
	var rand = randf()
	var cumulative_chance = 0.0
	
	cumulative_chance += xxlarge_chance
	if rand < cumulative_chance:
		return 5  # 超超大经验
	
	cumulative_chance += xlarge_chance
	if rand < cumulative_chance:
		return 4  # 超大经验
	
	cumulative_chance += large_chance
	if rand < cumulative_chance:
		return 3  # 大经验
	
	cumulative_chance += medium_chance
	if rand < cumulative_chance:
		return 2  # 中经验
	
	return 1  # 小经验

# 根据经验类型获取经验值
func get_experience_value(exp_type: int) -> int:
	match exp_type:
		1: return small_exp_value
		2: return medium_exp_value
		3: return large_exp_value
		4: return xlarge_exp_value
		5: return xxlarge_exp_value
		_: return small_exp_value

# 经验宝石被收集时的回调
func _on_experience_collected(value: int):
	if game_manager and game_manager.has_method("add_experience"):
		game_manager.add_experience(value)

# 在指定位置生成金币
func spawn_coin(position: Vector2, value: int = 1):
	if coin_scene == null:
		return null
		
	var coin_instance = coin_scene.instantiate()
	add_child(coin_instance)
	coin_instance.global_position = position
	
	# 设置金币价值（如果有相应方法）
	if coin_instance.has_method("set_value"):
		coin_instance.set_value(value)
		
	return coin_instance

# 在指定位置生成生命药水
func spawn_health_potion(position: Vector2, heal_amount: int = 20):
	if health_potion_scene == null:
		return null
		
	var potion_instance = health_potion_scene.instantiate()
	add_child(potion_instance)
	potion_instance.global_position = position
	
	# 设置治疗量（如果有相应方法）
	if potion_instance.has_method("set_heal_amount"):
		potion_instance.set_heal_amount(heal_amount)
		
	return potion_instance 
